<template>
  <div class="app-container">
    <el-card shadow="never">
      <el-breadcrumb separator-class="el-icon-arrow-right" v-if="backState">
        <el-breadcrumb-item>学科管理</el-breadcrumb-item>
        <el-breadcrumb-item>{{ this.$route.query.name }}</el-breadcrumb-item>
        <el-breadcrumb-item>目录</el-breadcrumb-item>
      </el-breadcrumb>
      <el-divider v-if="backState"></el-divider>
      <div>
        <div class="self-left">
          <el-form
            ref="form"
            :inline="true"
            :model="formInline"
            class="left demo-form-inline"
          >
            <el-form-item label="目录名称" prop="directoryName">
              <el-input v-model="formInline.directoryName"></el-input>
            </el-form-item>
            <el-form-item label="状态" prop="state">
              <el-select v-model="formInline.state" placeholder="请选择">
                <el-option label="已启用" value="1"></el-option>
                <el-option label="已禁用" value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button @click="reset">清除</el-button>
              <el-button type="primary" @click="onSearch">搜索</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="self-right">
          <el-button
            type="text"
            icon="el-icon-back"
            v-if="backState"
            @click="$router.back()"
            >返回学科</el-button
          >
          <el-button type="success" icon="el-icon-edit" @click="newAdd"
            >新增目录</el-button
          >
        </div>
      </div>
      <!-- 数据记录 -->
      <el-alert
        v-if="alertText !== ''"
        :title="'数据一共' + alertText + '条'"
        type="info"
        class="alert"
        :closable="false"
        show-icon
      ></el-alert>
      <!-- 表格数据 -->
      <div class="table-list">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" label="序号"> </el-table-column>
          <el-table-column prop="subjectName" label="所属学科">
          </el-table-column>
          <el-table-column prop="directoryName" label="目录名称">
          </el-table-column>
          <el-table-column prop="username" label="创建者"> </el-table-column>
          <el-table-column prop="addDate" label="创建日期">
            <template slot-scope="scope">{{
              formatDate(scope.row.addDate)
            }}</template>
          </el-table-column>
          <el-table-column prop="totals" label="面试题数量"> </el-table-column>
          <el-table-column prop="state" label="状态">
            <template slot-scope="scope">{{
              "已" + formatState(scope.row.state)
            }}</template>
          </el-table-column>
          <el-table-column prop="operation" label="操作">
            <template slot-scope="scope">
              <el-button type="text" @click="stateFn(scope.row)">{{
                scope.row.state === 1 ? "禁用" : "启用"
              }}</el-button>
              <el-button
                type="text"
                :disabled="scope.row.state === 1"
                @click="edit(scope.row)"
                >修改</el-button
              >
              <el-button type="text" :disabled="scope.row.state === 1" @click="remove(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        class="page"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="params.pagesize"
        layout="prev, pager, next,sizes, jumper"
        :total="alertText"
      >
      </el-pagination>
    </el-card>
    <directorysAdd
      :is-show="isShow"
      @closeDialog="closeDirDialog"
      :get-table-list="getTableList"
      :edit-data="editData"
      :subjectID="Number($route.query.id)"
    />
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/directorys'
import { status } from '@/api/hmmm/constants'
import dayjs from 'dayjs'
import directorysAdd from '@/module-hmmm/components/directorys-add'
export default {
  data () {
    return {
      formInline: {
        directoryName: '',
        state: null
        // subjectID: this.$route.query.id
      },
      alertText: null,
      params: {
        page: 1,
        pagesize: 10
      },
      tableData: [],
      editData: null, // 修改传值
      isShow: false,
      backState: false
    }
  },
  components: {
    directorysAdd
  },
  methods: {
    async getTableList (param) {
      const { data } = await list(param)
      this.tableData = data.items
      this.alertText = data.counts
    },

    // 搜索
    onSearch () {
      if (this.$route.query.id) {
        this.getTableList({
          page: 1,
          pagesize: this.params.pagesize,
          ...this.formInline,
          subjectID: this.$route.query.id
        })
      } else {
        this.getTableList({
          page: 1,
          pagesize: this.params.pagesize,
          ...this.formInline
        })
      }
      // this.reset()
    },
    // 点击清空
    reset () {
      this.$refs.form.resetFields()
    },
    // 分页
    handleSizeChange (pagesize) {
      // 点击每页条数触发
      this.params.pagesize = pagesize
      this.params.page = 1
      if (this.$route.query) {
        this.getTableList({ ...this.params, ...this.formInline, subjectID: this.$route.query.id })
      } else if (this.formInline.directoryName) {
        this.getTableList({ ...this.params, ...this.formInline })
      } else {
        this.getTableList({ ...this.params })
      }
    },
    handleCurrentChange (page) {
      // 点击页数 上页下页触发
      this.params.page = page
      if (this.$route.query) {
        this.getTableList({ ...this.params, ...this.formInline, subjectID: this.$route.query.id })
      } else if (this.formInline.directoryName) {
        this.getTableList({ ...this.params, ...this.formInline })
      } else {
        this.getTableList({ ...this.params })
      }
    },
    // 新增目录
    newAdd () {
      this.isShow = true
    },
    // 修改目录
    edit (row) {
      this.isShow = true
      this.editData = row.id
    },
    // 删除目录
    remove (row) {
      this.$confirm('此操作将永久删除该目录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await remove(row)
          this.getTableList({ page: 1, pagesize: 10 })
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 时间格式化
    formatDate (val) {
      return dayjs(val).format('YYYY-MM-DD hh:mm:ss')
    },
    // 状态格式化
    formatState (data) {
      const map = []
      status.forEach((item) => {
        map[item.value] = item.label
      })
      return map[data]
    },
    // 点击切换状态
    async stateFn (data) {
      switch (data.state) {
        case 0:
          data.state = 1
          break
        case 1:
          data.state = 0
          break
      }
      const res = await changeState({ id: data.id, state: data.state })
      if (res.status === 200) {
        this.$message.success('操作成功')
      }
    },

    // 关闭弹窗
    closeDirDialog () {
      this.isShow = false
      this.editData = null
    }
  },
  mounted () {
    if (this.$route.query.id) {
      this.backState = true
    } else {
      this.backState = false
    }
    // 数据列表(如果页面是从学科进入)
    if (this.$route.query.id) {
      const params = {
        page: 1,
        pagesize: 10,
        subjectID: this.$route.query.id
      }
      // 如果时学科分类进入，赋值给subjectID 传给子组件
      // this.subjectID = this.$route.query.id
      this.getTableList(params)
    } else {
      this.getTableList(this.params)
    }
  },
  watch: {
    // 路由地址path不变的情况下 vue会复用对应的组件 不会进行重新渲染
    // 如果想要执行拉取接口操作使用watch 监听$route
    '$route.query': function () {
      // console.log(this.$route.query.id)
      this.getTableList(this.params)
      if (this.$route.query.id) {
        this.backState = true
      } else {
        this.backState = false
      }
    }
  }
}
</script>

<style scoped lang='scss'>
.self-left {
  float: left;
}
.self-right {
  float: right;
}
.table-list {
  margin-top: 10px;
}
.page {
  text-align: right;
  margin-top: 20px;
}
</style>
